package com.dal.interceptor;

import com.dal.module.base.BaseController;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.sojoys.artifact.exception.AbstractLogicModelException;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

/**
 * @author : DengYing
 * @CreateDate : 2017年6月21日 上午10:49:04
 * @Description ：异常拦截器
 */
public class ExceptionInterceptor implements Interceptor {
	static Log log = LogFactory.get(ExceptionInterceptor.class);
	@Override
	public void intercept(Invocation inv) {
		try {
            inv.invoke();
        } catch (Exception e) {
        	 BaseController controller = (BaseController) inv.getController();
            if(e instanceof AbstractLogicModelException){
            	e.printStackTrace();;
            	log.error("AbstractLogicModelException {} {}",((AbstractLogicModelException) e).getCode(),e.getMessage());
                AbstractLogicModelException le = (AbstractLogicModelException) e;
                controller.renderError(le.getCode(), le.getMessage());
            }else {
				e.printStackTrace();;
            	log.error(e);
            	controller.renderError(-1, "服务器错误");
            }
        }
	}

}
